package com.sinszm;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sinszm.domain.JsonNodeData;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
 * Json内容读写工具
 *
 * @author admin
 * @date 2022/01/25
 */
public class DataJsonUtils {

    /**
     * 节点树格式
     *
     * @param list 列表
     * @return {@link List < JsonNodeData >}
     */
    private static List<JsonNodeData> nodeTreeFormat(List<JsonNodeData> list) {
        if (ObjectUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        //存储结构转换一下
        Map<String, JsonNodeData> map = list.stream()
                .filter(Objects::nonNull)
                .filter(db -> StrUtil.isNotEmpty(db.getId()))
                .collect(toMap(JsonNodeData::getId, a -> a, (k1, k2) -> k1));
        //返回树
        List<JsonNodeData> result = new ArrayList<>();
        //遍历处理存储树结构逻辑
        for (JsonNodeData tree : list) {
            if (StrUtil.isNotEmpty(tree.getPid())) {
                JsonNodeData temp = map.get(tree.getPid());
                if (temp == null) {
                    continue;
                }
                List<JsonNodeData> childList = temp.getChild();
                childList.add(tree);
                temp.setChild(childList);
                continue;
            }
            result.add(tree);
        }
        return result;
    }

    /**
     * json格式
     *
     * @param data 数据
     * @param list 列表
     */
    private static void jsonFormat(JsonNodeData data, List<Map<String, Object>> list) {
        if (data == null || list == null) {
            return;
        }
        Map<String,Object> map = MapUtil.builder(new TreeMap<String,Object>())
                .put(data.getName(), data.getValue())
                .build();
        if (ObjectUtil.isNotEmpty(data.getChild())) {
            List<Map<String, Object>> childList = new ArrayList<>();
            for (JsonNodeData childNodeData : data.getChild()) {
                jsonFormat(childNodeData, childList);
            }
            map.put("childData", childList);
        }
        list.add(map);
    }

    /**
     * 创建json文本
     *
     * @param list 列表
     * @return {@link String}
     */
    public static String createJsonText(List<JsonNodeData> list) {
        if (ObjectUtil.isEmpty(list)) {
            return "{}";
        }
        List<JsonNodeData> nodeList = nodeTreeFormat(
                Optional.ofNullable(list)
                        .orElse(new ArrayList<>())
                        .stream()
                        .peek(d -> d.setChild(d.getChild() == null ? new ArrayList<>() : d.getChild()))
                        .collect(Collectors.toList())
        );
        if (nodeList.isEmpty()) {
            return "{}";
        }
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (JsonNodeData jsonNodeData : nodeList) {
            jsonFormat(jsonNodeData, resultList);
        }
        if (resultList.size() == 1) {
            return Json.toJson(resultList.get(0), JsonFormat.tidy());
        }
        return Json.toJson(resultList, JsonFormat.tidy());
    }

}
